package web;

import web.beans.UserBean;
import util.Context;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by Новак on 30.11.2014.
 */
public class PermissionsServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    public PermissionsServlet() {
        super();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            String resultPage = null;
            HttpSession session = request.getSession();
            UserBean userBean = (UserBean) session.getAttribute("userBean");


            System.out.println(userBean.getLogin());
            System.out.println(userBean.getAccessLevel());
            System.out.println(request.getRequestURL());
            int tempAccesLevel = userBean.getAccessLevel();
            if (tempAccesLevel != 0) {
                resultPage = String.valueOf(request.getRequestURI());
                String strAfter = resultPage.replace("/faces", "");
                resultPage = strAfter;

            } else {
                System.out.println(tempAccesLevel);
                resultPage = "/Login/authorization.xhtml";
            }

//            if(userBean.getAccessLevel()<1) {
            //  resultPage = "/Login/authorization.xhtml";
//            }
//            else{
//                resultPage = request.getRequestURI();
//            }

//            if (userBean == null) {
//
//                resultPage = "/Login/authorization.xhtml";
//
//            } else if (userBean.getAccessLevel() == 0) {
//                resultPage = "/Login/authorization.xhtml";
//
//            } else if (userBean.getAccessLevel() == 1) {
//                System.out.println(userBean.getId());
//                System.out.println(userBean.getLogin());
//                resultPage = "/MyAccount/myAccount.xhtml";
//
//            } else {
//                System.out.println(userBean.getId());
//                System.out.println(userBean.getLogin());
//                resultPage = request.getRequestURI();
//
//            }
            ServletContext SC = getServletContext();
            RequestDispatcher Disp = SC.getRequestDispatcher(resultPage);

            Disp.forward(request, response);
        } catch (Throwable theException) {
            theException.printStackTrace();
        }
    }

    public void transition(String resultPage, HttpServletRequest request, HttpServletResponse response) {
        ServletContext SC = getServletContext();
        RequestDispatcher Disp = SC.getRequestDispatcher(resultPage);
        try {
            Disp.forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
